let STA = require('STA');

cc.Class({
    extends: cc.Component,

    properties: {
        BigChapterCube: cc.Prefab,
        bigChapterCubesContent: cc.Node,  // 大关方块容器
        starWord:cc.Node,
        keyWord:cc.Node,
        tapSound:cc.AudioClip,
        testText:cc.Node,
    },

    showLog(data){
        this.testText.getComponent(cc.Label).string = data;
    },


    onLoad () {
        this.showLog(888);

        this.perBigChapSmallNumber = 15; // 每个大关所拥有的小关


        this.allChaptersNumber = 300; // 总小关数
        this.allBigChaptersNumber = Math.ceil(this.allChaptersNumber / this.perBigChapSmallNumber); // 总大关数

        this.couldPlayChaptersNumber = 105; // 可玩小关数
        this.couldPlayBigChaptersNumber = Math.ceil(this.couldPlayChaptersNumber / this.perBigChapSmallNumber); // 可玩大关数
        
        this.lineNumber = 3;    // 每行显示多少个大关

        this.cubeWidth = 260;
        this.cubeHeight = 340;

        this.cubeGap = 50;
        this.widthUnit = this.cubeWidth + this.cubeGap;
        this.heightUnit = this.cubeHeight + this.cubeGap;

        this.bigChapterCubesContent.height = Math.ceil(this.allBigChaptersNumber / this.lineNumber) * this.heightUnit;
        this.starWord.getComponent(cc.Label).string = "完美过关："+(STA.perfectNumber)+"/"+this.allChaptersNumber;
        this.keyWord.getComponent(cc.Label).string = "成功解锁："+(STA.bigChapterIndex+1)+"/"+this.allBigChaptersNumber;


        // 需要添加滚动
        for(let i=0;i<this.allBigChaptersNumber;i++){
            let theX = i % this.lineNumber,
                theY = Math.floor(i / this.lineNumber);

            let cube = cc.instantiate(this.BigChapterCube);
            this.bigChapterCubesContent.addChild(cube);

            cube.width = this.cubeWidth;
            cube.height = this.cubeHeight;
            cube.x = theX * this.widthUnit;
            cube.y = -theY * this.heightUnit - 1/3 * this.heightUnit;

            STA.log(STA.bigChapterIndex);
            let isLock = i>STA.bigChapterIndex;
            let filename = isLock?"lock":STA.chapterAnimals[i].filename;

            cc.loader.loadRes("animals/"+filename,cc.SpriteFrame,(err,spriteFrame)=>{
                if(err) cc.error(err);
                let node = new cc.Node();
                node.addComponent(cc.Sprite).spriteFrame = spriteFrame;
                node.y += 15; // 调整图片位置
                cube.addChild(node);
                
                if(i>=this.couldPlayBigChaptersNumber){  // 开发中的关卡
                    cube.getChildByName("name").getComponent(cc.Label).string = "敬请期待";
                }else{ // 已开放的大关卡
                    if(isLock){ // 未解锁的大关
                        cube.getChildByName("name").getComponent(cc.Label).string = "未解锁";
                    }else{ // 已解锁的大关
                        cube.getChildByName("name").getComponent(cc.Label).string = STA.chapterAnimals[i].name;
                        cube.on(cc.Node.EventType.TOUCH_START, (event)=>{
                            cube.runAction(cc.sequence(
                                cc.scaleTo(0.2,1.1),
                                cc.scaleTo(0.2,1),
                                cc.callFunc(()=>{
                                    STA.bigChapterNumber = i+1;
                                    cc.director.preloadScene("smallChapters", function () {
                                        cc.director.loadScene("smallChapters");
                                    });
                                })
                            ));
                            STA.playAudio(this.tapSound);
                        });
    
                    }
                }
            });
        }

        this.bigChapterCubesContent.x += -1/2 * (this.lineNumber * this.widthUnit - this.widthUnit); // 水平调整
        
    },

    start () {

    },
    
    share(){
        STA.share();
    },

    back(){
        STA.playAudio(this.tapSound);
        cc.director.loadScene("mainStart");
    },

    update (dt) {},
});
